$(document).ready(function() {    

    
    // --- Modal ---
    $( '#msgDialog' ).dialog({
		draggable:false,
        autoOpen: false,
		resizable: false,
		height:400,
		modal: true,
        buttons: {
            'Ok': function() {
                $( this ).dialog( 'close' );
            }
        }
    });
    
    // --- Confirm Deletion ---
    $( 'a.confirm' ).click(function(e){
		
        e.preventDefault();
        var theHREF = $(this).attr("href");
		var title = $(this).attr("title");
		var target = $(this).attr('target');

        if(!title ){
			$( '#msgDialog > p' ).html( 'Are You Sure?' );
		}else{
			$( '#msgDialog > p' ).html( title );
		}
		
        $( '#msgDialog' ).dialog( 'option', 'title', 'Confirm' );
        $("#msgDialog").dialog('option', 'buttons', {
                "Confirm" : function() {
						
						if(typeof  target == "undefined")
						{
							window.location.href = theHREF;
						}else{
							$(this).dialog("close");
							window.open(theHREF);
						}
                    },
                "Cancel" : function() {
						$(this).dialog("close");

                    }
                });
		
		$("#msgDialog").dialog("open");
		$('.ui-dialog-buttonpane').find('button').addClass('btn btn-mini btn-primary');

    });


	//function confirm-auto start
	$( 'a.confirm-auto' ).click(function(e){
			e.preventDefault();
			$('body').addClass('disable_scroll');
			$(this).addClass('current_pick');
			
			var theHREF = $(this).attr("href");
			var title = $(this).attr("title");
			if(!title ){
				$( '#msgDialog > p' ).html( 'Are You Sure?' );
			}else{
				$( '#msgDialog > p' ).html( title );
			}
			$('#msgDialog' ).dialog( 'option', 'title', 'Confirm Action' );
			$('#msgDialog' ).dialog( {
				resizable: false,
				height:140,
				modal: true
			});
			
			$("#msgDialog").dialog('option', 'buttons', {
					"Confirm" : function() {
						$('body').removeClass('disable_scroll');
						$(this).dialog("close");
							$.ajax({
									type: 'POST',
									url: theHREF,
									data:{
										'jquery' : true
									},
									beforeSend: function() {
										$('body').addClass('disable_scroll');
										$('#loader').show();
									},
									 complete: function(){
										$('body').removeClass('disable_scroll');
										$('#loader').hide('slow');
									},
									success: function(msg){
										if(msg == 'true')
										{
											alert("Process was successfully Done");
											$('a.current_pick').parents('tr.profile_box').hide(2000);
										}else{
											alert("Process encountered an error");
										}
									},
									error: function (msg){
										alert("An error has occured");
									}
							});
						},
					"Cancel" : function() {
						$('body').removeClass('disable_scroll');
						$(this).dialog("close");
						}
			});

			$("#msgDialog").dialog("open");
    });
	
	$('.redirect-auto').on('change',function(e){
		e.preventDefault();
		var url= $(this).attr('url');
		var segment = $(this).val();
		var doit = function(url,segment){
			window.location.href = url+"/"+segment
		};

		$("#msgDialog").dialog('option','height',200);
		$( '#msgDialog > p' ).html( 'Please Wait while system Generates Billing.' );
		$('.ui-dialog-buttonpane').find('button').remove();
		$('.ui-dialog-titlebar').find('button').remove();
		$( '#msgDialog > div.content' ).text('Please Wait.');
		$(".ui-dialog-titlebar-close").hide();
		 $("#msgDialog").dialog('option','buttons','');
		$( '#msgDialog > p' ).text( 'Redirecting' );
		$("#msgDialog").dialog("open");
		
		setInterval(doit(url,segment),1000);
	});
});// --- end document ready ---


